package com.hdoit.pisces.web.other.controller;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.hdoit.pisces.logical.rel.model.Event;
import com.hdoit.pisces.logical.rel.service.IEventService;
import com.hdoit.pisces.logical.viewmodel.EventSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;

@Controller
@RequestMapping(value="/admin/event/")
public class EventController extends BaseController{
	
	@Override
	public String getRoutePath(){
		return "areas/admin/event/";
	}
	
	private IEventService eventService;
	
	public	IEventService getEventService() {
		return eventService;
	}
	
	@Resource
	public void setEventService(IEventService eventService){
		this.eventService=eventService;
	}
	
	private final String indexViewName="index";
	private final String outputViewName="output";
	private final String detailedViewName="eventDetailed";
	private final String saveViewName="save";
	private final String deleteViewName= "delete";
	
	@RequestMapping(value=indexViewName,method= {RequestMethod.GET,RequestMethod.POST})
	public String index(Model model,PagerModel pagerModel,EventSearch search,boolean ajaxPost) throws IOException{
		pagerModel.setTotalAmount(eventService.queryCount(search));
		List<Event> events=eventService.queryList(search,pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("events",events);
		model.addAttribute("eventSearch", search);
		if(ajaxPost){
		      return view(outputViewName);
		    }
		return view(indexViewName);
	}
		
	@RequestMapping(value=detailedViewName,method ={RequestMethod.GET})
	public String evetDetailed(Model model,String key){
		Event event=new Event();
		if(!StringUtils.isEmpty(key)){
			event=eventService.queryModel(key);
		}
		model.addAttribute("event",event);
		return view(detailedViewName);
	}
	
	@RequestMapping(value=saveViewName, method ={ RequestMethod.POST})
	public String save(Model model,Event event){
		if(StringUtils.isEmpty(event.getEventId())){
			event.setEventId(CommonUtils.getKeyId());
//			event.setCreateDate(DateUtils.getCurrent());
//			user.setDeleteMark(YesOrNoInteger.no);
//			event.setCreateUserId(getLoginModel().getLoginId());
//			event.setCreateUserName(getLoginModel().getLoginName());
			event.setModelStatus(ModelStatus.add);
		}else{
//			event.setModifyDate(DateUtils.getCurrent());
//			event.setModifyUserId(getLoginModel().getLoginId());
//			event.setModifyUserName(getLoginModel().getLoginName());
			event.setModelStatus(ModelStatus.update);
		}
		ActionResult actionResult;
		if(eventService.applyChange(event)){
			actionResult=new ActionResult();
		}else{
			actionResult=new ErrorResult();
		}
		model.addAttribute("event",event);
		model.addAttribute(ManageContainer.RESULTKEY,actionResult);
		return view(detailedViewName);
	}
	

	
//	@RequestMapping(value=deleteViewName,method= {RequestMethod.POST},produces="text/plain;charset=utf-8"
//			public@ResponseBodyStringdelete(String key){
//		if(userService.deleteFakeByKey(key)){
//			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
//		}
//		return new JsonActionResult(ManageContainer.ERROR).toJson();
//	}
}